package com.itcast.tcp.server.handler;

import com.itcast.tcp.protocol.MyProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @program: bio_tcp
 * @description:
 * @author: zhanghz001
 * @create: 2021-02-23 11:07
 **/
public class ServerTcpPackageHandler extends SimpleChannelInboundHandler<MyProtocol> {
    
    private int count = 0;
    
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext,
                                MyProtocol myProtocol) throws Exception {
        System.out.println("服务器接收到的消息: " +
                new String(myProtocol.getBytes(), CharsetUtil.UTF_8));
        System.out.println("服务器接收到的消息数量: " + (++count));
        
        //
        byte[] bytes = "ok".getBytes();
        MyProtocol protocol = new MyProtocol();
        protocol.setBytes(bytes);
        protocol.setLength(bytes.length);
        channelHandlerContext.writeAndFlush(protocol);
    }
}
